
#include "Key.h"

/** Set of possible key names for debugging and error messages. */
const char *Key::key_type_names[] = { "KEY_TYPE_KEY",
                                      "KEY_TYPE_PUBLIC_RSA",
                                      "KEY_TYPE_PRIVATE_RSA",
                                      "KEY_TYPE_PUBLIC_DSA",
                                      "KEY_TYPE_PRIVATE_DSA",
                                      "KEY_TYPE_PUBLIC_ECDSA",
                                      "KEY_TYPE_PRIVATE_ECDSA",
                                      "KEY_TYPE_SYMM_AES",
                                      "KEY_TYPE_SYMM_DES" };

/** Set of possible key abbreviations. */
const char *Key::key_type_abbrs[] = { "key",
                                      "PublicRSA",
                                      "PrivateRSA",
                                      "PublicDSA",
                                      "PrivateDSA",
                                      "PublicECDSA",
                                      "PrivateECDSA",
                                      "SymmAES",
                                      "SymmDES" };

/** Cryptographic key destructor, destroys 'keyname' string. */
Key::~Key()
{
   if (keyname) free((void *)keyname);
   keyname = NULL;
}

/** Get the key type flag, in this case KEY_TYPE_KEY.
 * 
 * @return
 * Return value is Key::KEY_TYPE_KEY. */
int Key::getKeyType()
{
   return KEY_TYPE_KEY;
}

/** Get a string description of the key type for debug or error message
 * purposes, usually these are the names of the constant flags denoting
 * key type.
 * 
 * @return
 * Static string reading the full name of the key type. */
const char *Key::getKeyTypeFullName()
{
   return key_type_names[getKeyType()];
}

/** Get an abbreviated string descrption of the key type, for the purposes of
 * saving keys to a database labelled with their type.
 * 
 * @return
 * Static string reading the abbreviated name of the key type. */
const char *Key::getKeyTypeAbbrName()
{
   return key_type_abbrs[getKeyType()];
}

/** Get the name of the key.
 * 
 * @return
 * Pointer to the internal key name string. */
const char *Key::getKeyName()
{
   return keyname;
}

/** Set the name of the key.
 * 
 * @param tname
 * The name to set the internal key name to. */
void Key::setKeyName(const char *tname)
{
   if (keyname) free((void *)keyname);
   keyname = (tname) ? (const char *)strdup(tname) : NULL;
}
